// JavaScript Document
var xmlDoc
try
{
    xmlDoc = loadXMLDoc('XML/default.xml');
}
catch(e)
{
    alert(" Error: File not found: "+e.message);
}
// Function to save the record to the XML file
function saveTask()
{
}

// Function to update the record in the XML file
function updateTask()
{
}

// Function to delete a record from the XML file
function delTask()
{
}

// Funtion to load task to a HTML file
function loadTask(tag,n,m)
{
    var val = xmlDoc.getElementsByTagName(tag)[n].childNodes[m].childNodes[0].nodeValue;
    return val;
}

// Function loadTask overloaded
function loadElements(tag)
{
    var val = xmlDoc.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
    return val;
}
//======================================================================================================================

// Global Variables
var tasks;

function storageManager(n)
{
    switch(n)
    {
        case 0: firstLoad();
        break;
        default: alert("Error in StorageManager()");
    }
}

//--------------------------------------------------------------------------------------------------------------------
function firstLoad()
{
    //alert("Setting Initial Values of the variables");
    setInitialValues();
    //alert("Restoring Saved Tasks");
    readFullXML();
}

// Function to read the XML file and set the initial values of the variables
function setInitialValues()
{
    tasks   = loadElements('tasks');
    setT(parseInt(tasks)+1);
}

// Function to read the entire Tasklist from the XML file
function readFullXML()
{
    var cross = new Array();
    for(i=0;i<tasks;i++)
    {
        var val = new Array();
        var indx = 0;   
        for(j=1;j<16;j=j+2) 
        {
            val[indx] = loadTask('task',i,j);
            //alert("j= "+j+"  Cell= "+cell+"  val= "+val[cell]);
            indx++;
        }
    
        // getting object to reference a row 
        var tbl = document.getElementById("tasks").insertRow(1);
        
        // getting objects to reference cloumns
        var sr_no = tbl.insertCell(0);
    	var tsk = tbl.insertCell(1);
    	var stat = tbl.insertCell(2);
    	var prio = tbl.insertCell(3);
    	var ts = tbl.insertCell(4);
    	var dl = tbl.insertCell(5);
    	var cng = tbl.insertCell(6);
    	var rmov = tbl.insertCell(7);
        
        // Setting Row ID for unique reference
    	tbl.id =  val[0];
        
        // Inserting Task data into the cells
    	sr_no.innerHTML    = "<b>"+val[1]+"</b>";  
    	tsk.innerHTML      = "<b>"+val[2]+"</b>";
    	stat.innerHTML     = "<b>"+val[3]+"</b>";
    	prio.innerHTML     = "<b>"+val[4]+"</b>";
    	ts.innerHTML       = "<b>"+val[5]+"</b>";
    	dl.innerHTML       = "<b>"+val[6]+"</b>";
    	if(val[7]=="nc")
    	{
            cng.innerHTML  = "<b>Complete</b>";
        }
        else
        {
            cng.innerHTML  = "<b>undo</b>";
        }
        rmov.innerHTML     ="<img src='images/Del.png' height='20' width='50'>";
        
       	// Setting adjacency matrix value to nc="not crossed". Representing New Task
        cross[tbl.id]=val[7];
    }
    setCross(cross);
}
//---------------------------------------------------------------------------------------------------------------------
